package com.mb.ext.web.util;


import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @Description
 * @Author 孙浩
 * @Version V1.0.0
 * @Date 2020/12/22 0022
 */
public class DateUtil {

  public static Long invitationTime = 20L;

  public static Long refInvitationTime = 30L;

  public static long todaySeconds() {
    return todayMills() / 1000;
  }

  public static String yyyyMMdd(String pattern) {
    return format("yyyy-MM-dd");
  }

  public static String format(int relative, String pattern) {
    Date date = DateUtil.relative(relative);
    return format(date, pattern);
  }

  public static String format(String pattern) {
    return format(new Date(), pattern);
  }

  /**
   * 格式化
   *
   * @param date
   * @param pattern
   * @return
   */
  public static String format(Date date, String pattern) {
    try {
      SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
      dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
      return dateFormat.format(date);
    } catch (Exception e) {
      return "";
    }
  }

  public static String today(String pattern) {
    long dayMills = todayMills();
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    return dateFormat.format(dayMills);
  }

  public static long todayMills() {
    return relative(0).getTime();
  }

  public static Date parse(String s) throws ParseException {
    return parse(s, "yyyy-MM-dd HH:mm:ss");
  }

  public static Date parse(String s, String pattern) throws ParseException {
    if(StringUtils.isEmpty(s)){
      return null;
    }
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
    return dateFormat.parse(s);
  }

  public static String parseStr(String s, String pattern) throws ParseException {
    return format(parse(s,pattern),pattern);
  }


  public static String parseData(Date data, String pattern) throws ParseException {
    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    return  dateFormat.format(data);
  }


  public static List<String> days(String min, String max) throws ParseException {
    Date minDate = parse(min, "yyyy-MM-dd HH:mm:ss");
    Date maxDate = parse(max, "yyyy-MM-dd HH:mm:ss");

    List<String> days = new ArrayList<>();
    while (minDate.getTime() <= maxDate.getTime()) {

      days.add(DateUtil.format(minDate, "yyyy-MM-dd"));

      Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
      calendar.setTime(minDate);
      calendar.add(Calendar.DATE, 1);
      minDate = calendar.getTime();
    }
    return days;
  }

  /**
 *字符串的日期格式的计算
*/
  public static int daysBetween(String smdate,String bdate) throws ParseException{
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    Calendar cal = Calendar.getInstance();
    cal.setTime(sdf.parse(smdate));
    long time1 = cal.getTimeInMillis();
    cal.setTime(sdf.parse(bdate));
    long time2 = cal.getTimeInMillis();
    long between_days=(time2-time1)/(1000*3600*24);

    return Integer.parseInt(String.valueOf(between_days));
  }

  public static void main(String[] args) throws ParseException {
    int i = daysBetween("2021-04-06", "2021-04-07");
    System.out.println(i);
  }

  public static long minBetween(Date startData){
    long start = startData.getTime();
    long end = System.currentTimeMillis();
    long min = (end - start) / (1000 * 60);
    return min;
  }


  public static String relative(int day, String pattern) {
    Date date = relative(day);
    return format(date, pattern);
  }

  public static Date relative(int day) {
    Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
    if (day != 0) {
      calendar.add(Calendar.DATE, day);
    }
    return calendar.getTime();
  }

  public static int parseStrInt(String s, String pattern) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
    return Integer.parseInt( simpleDateFormat.format(simpleDateFormat.parse(s)));
  }




  static final int MINUTES_PER_HOUR = 60;
  static final int SECONDS_PER_MINUTE = 60;
  static final int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;

  private static long[] getTime(LocalDateTime dob, LocalDateTime now) {
    LocalDateTime today = LocalDateTime.of(now.getYear(),
        now.getMonthValue(), now.getDayOfMonth(), dob.getHour(), dob.getMinute(), dob.getSecond());
    Duration duration = Duration.between(today, now);

    long seconds = duration.getSeconds();

    long hours = seconds / SECONDS_PER_HOUR;
    long minutes = ((seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
    long secs = (seconds % SECONDS_PER_MINUTE);

    return new long[]{hours, minutes, secs};
  }


  /**
   * 指定日期加上天数后的日期
   * @param num 为增加的天数
   * @param currdate 创建时间
   * @return
   * @throws ParseException
   */
  public static String plusDay(int num, Date  currdate) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("现在的日期是：" + format.format(currdate));
    Calendar ca = Calendar.getInstance();
    ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
    currdate = ca.getTime();
    String enddate = format.format(currdate);
    System.out.println("增加天数以后的日期：" + enddate);
    return enddate;
  }


  /**
   * 相对日期列表
   *
   * @param day
   * @param pattern
   * @return
   */
  public static List<String> relativeDates(int day, String pattern) {
    List<String> formats = new ArrayList<>();

    List<Date> dates = relativeDates(day);
    for (Date date : dates) {
      String format = format(date, pattern);
      formats.add(format);
    }

    return formats;

  }



  public static List<Date> relativeDates(int day) {

    if (day > 0) {
      List<Date> dates = new ArrayList<>();
      for (int i = 0; i < day; i++) {
        dates.add(relative(i));
      }
      return dates;
    }

    if (day < 0) {
      List<Date> dates = new ArrayList<>();
      for (int i = day + 1; i <= 0; i++) {
        dates.add(relative(i));
      }
      return dates;
    }
    return Arrays.asList(today());
  }

  public static Date today() {
    return relative(0);
  }

  public static Date yesterday() {
    return relative(-1);
  }

  public static Date tomorrow() {
    return relative(1);
  }

  public static Date getNowDate(){
    return new Date();
  }
}
